package com.base.web.core.basic.service;

import com.base.web.core.basic.mapper.BasicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description: 通用Service接口实现
 *
 * @Author: rentie
 * @Date: 2018/8/9 14:39
 */
public abstract class BasicServiceImpl<T> implements BasicService<T> {

    @Autowired
    private BasicMapper<T> basicMapper;

    @Override
    public List<T> select(T record) {
        return basicMapper.select(record);
    }

    @Override
    public T selectByPrimaryKey(Object key) {
        return basicMapper.selectByPrimaryKey(key);
    }

    @Override
    public List<T> selectAll() {
        return basicMapper.selectAll();
    }

    @Override
    public T selectOne(T record) {
        return basicMapper.selectOne(record);
    }

    @Override
    public Integer selectCount(T record) {
        return basicMapper.selectCount(record);
    }

    @Override
    public Integer insert(T record) {
        return basicMapper.insert(record);
    }

    @Override
    public Integer insertSelective(T record) {
        return basicMapper.insertSelective(record);
    }

    @Override
    public Integer updateByPrimaryKey(T record) {
        return basicMapper.updateByPrimaryKey(record);
    }

    @Override
    public Integer updateByPrimaryKeySelective(T record) {
        return basicMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer delete(T record) {
        return basicMapper.delete(record);
    }

    @Override
    public Integer deleteByPrimaryKey(Object key) {
        return basicMapper.deleteByPrimaryKey(key);
    }

    /**
     * 条件分页查询
     *
     * @param record      实体类
     * @param currentPage 当前页
     * @param pageSize    没用显示数量
     * @return
     */
    @Override
    public PageInfo<T> select(T record, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<T> list = select(record);
        return new PageInfo<T>(list);
    }

    /**
     * 无条件分页查询
     *
     * @param currentPage 当前页
     * @param pageSize    没用显示数量
     * @return
     */
    @Override
    public PageInfo<T> selectAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<T> list = selectAll();
        return new PageInfo<T>(list);
    }

    /**
     * 条件分页查询 layui定制
     * @param record      实体类
     * @param map page 当前页   limit 每页数量
     * @return
     */
    @Override
    public PageInfo<T> select(T record,Map<String, String> map) {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize);
        List<T> list = select(record);
        return new PageInfo<T>(list);
    }

    /**
     * 无条件分页查询 layui定制
     *
     * @param map page 当前页   limit 每页数量
     * @return
     */
    @Override
    public PageInfo<T> selectAll(Map<String, String> map) {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize);
        List<T> list = selectAll();
        return new PageInfo<T>(list);
    }
}